package com.hfjava.practice03;

import com.hfjava.practice02.Student;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: com.hfjava.practice03
 * @ClassName: Dom4jParser
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/1/6 17:39
 * @Copyright 大牛版板所有
 */
public class Dom4jParser {
    static List<Student> studentList = new ArrayList<>();
    private static final String RELATIVE_PATH = "chapter06/data/Scores.xml";
    private static Document doc = null;

    // 打印
    public static void showInfo() throws Exception {
        SAXReader sr = new SAXReader();
        doc = sr.read(RELATIVE_PATH);
        Element element = doc.getRootElement();
        Iterator iter = element.elementIterator();
        while (iter.hasNext()){
            Element el = (Element)iter.next();
            System.out.println("学号："+el.attributeValue("id"));
            System.out.println("\t姓名："+el.elementTextTrim("name"));
            System.out.println("\t课程："+el.elementTextTrim("course"));
            System.out.println("\t分数："+el.elementTextTrim("score"));
        }
    }

    // 保存
    public static void saveXml() throws Exception {
        OutputFormat format = new OutputFormat();
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(RELATIVE_PATH),format);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

    // 删除
    public static void delete()throws Exception{
        SAXReader saxReader = new SAXReader();
        doc = saxReader.read(RELATIVE_PATH);
        Element element = doc.getRootElement();
        Iterator it = element.elementIterator();
        while (it.hasNext()){
            Element el = (Element)it.next();
            if (el.attributeValue("id").equals("1")){
                el.remove(el.element("score"));
            }
        }
        saveXml();
    }

    // 修改
    public static void modify() throws Exception {
        SAXReader saxReader = new SAXReader();
        doc = saxReader.read(RELATIVE_PATH);
        Element element = doc.getRootElement();
        Iterator it = element.elementIterator();
        while (it.hasNext()){
            Element el = (Element)it.next();
            if (el.attributeValue("id").equals("2")){
                el.element("score").setText("60");
            }
        }
        saveXml();
    }

    // 增加
    public static void add() throws Exception{
        SAXReader saxReader = new SAXReader();
        doc = saxReader.read(RELATIVE_PATH);
        Element element = doc.getRootElement();
        Element addStu = element.addElement("student");
        addStu.addAttribute("id","5");
        Element addName = addStu.addElement("name");
        addName.setText("王浩");
        Element courseName = addStu.addElement("course");
        courseName.setText("php");
        Element scoreName = addStu.addElement("score");
        scoreName.setText("82");

        saveXml();
    }

    public static void main(String[] args) throws Exception{
        //delete();
        //modify();
        add();
        showInfo();
    }
}
